package com.sitech.portal.server.action;

import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONArray;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.AppException;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.server.domain.Message;
import com.sitech.portal.server.form.MessageSettingForm;
import com.sitech.portal.server.service.IMessageCenterService;
import com.sitech.portal.server.service.IMessageSettingService;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;

/**
 * ClassName: com.sitech.ismp.portal.action.MessageCenterAction
 * @author liujhc
 * @since Mar 2, 2011
 */
public class MessageCenterAction extends JGridBaseAction {

	private IMessageCenterService messageCenterService;
	private IMessageSettingService msgSettingService;
	private String flag;
	private String responseId;
	private List<Message> responseList;
	private Message msg;
	private String responseDate;
	private String loginId;
	private MessageSettingForm settingForm;
	
    public IMessageCenterService getMessageCenterService() {
		return messageCenterService;
	}

	public void setMessageCenterService(IMessageCenterService messageCenterService) {
		this.messageCenterService = messageCenterService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public List<Message> getResponseList() {
		return responseList;
	}

	public void setResponseList(List<Message> responseList) {
		this.responseList = responseList;
	}

	public String getResponseId() {
		return responseId;
	}

	public void setResponseId(String responseId) {
		this.responseId = responseId;
	}

	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	public String getResponseDate() {
		return responseDate;
	}

	public void setResponseDate(String responseDate) {
		this.responseDate = responseDate;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public IMessageSettingService getMsgSettingService() {
		return msgSettingService;
	}

	public void setMsgSettingService(IMessageSettingService msgSettingService) {
		this.msgSettingService = msgSettingService;
	}

	public MessageSettingForm getSettingForm() {
		return settingForm;
	}

	public void setSettingForm(MessageSettingForm settingForm) {
		this.settingForm = settingForm;
	}

	/**
     * 加载未读消息
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String loadUnreadMessages() {
    	try{
	        String MSG_ID = this.getRequest().getParameter("MSG_ID");
	        if (MSG_ID == null || MSG_ID=="") {
	            MSG_ID = "0";
	        }
	        // 取得用户信息
	        TblSYS_LOGINMSG user = this.getCurrentUser();
	        settingForm = msgSettingService.queryMessageSettingByLoginId(this.getCurrentUser().getLOGIN_ID());
	        List<Message> list = messageCenterService.findUnreadMessage(user.getLOGIN_ID(), MSG_ID,settingForm);
	        JSONArray json = JSONArray.fromObject(list);
	        flag = json.toString();
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
        return SUCCESS;
    }

    /**
     * 把消息标识为已读
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String readMessage() {
    	flag = "true";
        String MSG_ID = this.getRequest().getParameter("MSG_ID");
        try{
            // 取得用户信息
            TblSYS_LOGINMSG user = this.getCurrentUser();
            messageCenterService.readMessage(MSG_ID, user.getLOGIN_ID());
        }catch(Exception e){
        	flag = "操作失败:"+e.getMessage();
        }
        return SUCCESS;
    }
    /**
     * 根据RESPONSE_ID获取该信息的回复内容
     * @return
     */
    public String loadMsgByResponseId(){
    	settingForm = msgSettingService.queryMessageSettingByLoginId(this.getCurrentUser().getLOGIN_ID());
    	responseList = messageCenterService.loadMsgByResponseId(responseId);
    	return SUCCESS;
    }
    
    public String loadOnlineServerMsg(){
    	settingForm = msgSettingService.queryMessageSettingByLoginId(this.getCurrentUser().getLOGIN_ID());
    	responseList = messageCenterService.loadOnlineServerMsg(msg,settingForm);
    	return SUCCESS;
    }
    /**
     * 保存消息
     * @return
     */
    public String saveMsg(){
    	try{
    		messageCenterService.saveMsg(msg);
    		flag = "true";
    	}catch(Exception ex){
    		flag = "false";
    	}
    	return SUCCESS;
    }
    /**
     * 加载在线客服用户
     * @return
     * @throws AppException
     */
    public String loadOnlineMsgUser() throws AppException{
    	try{
    		List<TblSYS_LOGINMSG> onlineMsgSenders = messageCenterService.loadOnlineMsgUser(this.getCurrentUser().LOGIN_ID,settingForm);
    		flag = JSONUtil.toJSON(onlineMsgSenders);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		flag = "false";
    		throw new AppException("加载在线用户信息异常",ex);
    	}
    	return SUCCESS;
    }
    /**
     * 查询消息显示设置
     * @return
     */
    public String queryMessageSetting(){
    	try{
    		settingForm = msgSettingService.queryMessageSettingByLoginId(this.getCurrentUser().getLOGIN_ID());
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
    	return SUCCESS;
    }
    
    /**
     * 查看消息历史记录
     * @return
     */
    public String loadMsgHisList() {
		return SUCCESS;
	}
    
    public String loadMsgHisListQueryJson() {
    	try{
	    	String user = this.getRequest().getParameter("msg.user");
	    	
	    	HashMap map = new HashMap();
	    	map.put("LOGIN_ID", this.getCurrentUser().LOGIN_ID);
	    	if(user!=null && user.length()>0)
	    		map.put("USER", user);
	    	
			IPage result = msgSettingService.findMsgHisList(map, this.getPageForm());
			this.setResult(result);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
		return SUCCESS;
	}
    
}
